﻿window.highlighter = new SearchTermHighlighter();
highlighter.init();
function SearchTermHighlighter() {
    var k = ((document.createElement) ? true : false);
    var l = false;
    var m = new Array();
    var n = 0;
    var q = new Array();
    var r = true;
    var t = false;
    this.parameters = 'key';
    this.addStyle = addStyle;
    this.init = init;
    this.loadSearchTerms = loadSearchTerms;
    this.highlight = highlight;
    this.highlightTerm = highlightTerm;
    this.unhighlight = unhighlight;
    this.wildcard = true;
    if (!k)
        return;
    loadDefaults();
    function loadDefaults() {
        var a = '#FF6 #BFF #9F9 #F99 #F6F'.split(/ /);
        var b = '#800 #0A0 #860 #049 #909'.split(/ /);
        for (var i = 0; i < a.length; i++) {
            q.push(new HighlightStyle('black', a[i], 'bold'));
        }
        for (i = 0; i < b.length; i++) {
            q.push(new HighlightStyle('white', b[i], 'bold'));
        }
    }
    function addStyle(a, b, c) {
        if (!k)
            return;
        if (r) {
            q = new Array();
            r = false;
        }
        q.push(new HighlightStyle(a, b, c));
    }
    function init() {
        if (!k)
            return;
        this.loadSearchTerms();
        var a = 0;
        document.write('<style type="text/css">\n');
        for (i = 0; i < m.length; i++) {
            document.write('.' + m[i].cssClass + '{' + q[a] + '}\n');
            a++;
            if (a >= q.length)
                a = 0;
        }
        document.write('</style>\n');
        l = true;
    }
    function loadSearchTerms() {
        var a = new Array();
        var c = getParamValues(document.location.href, this.parameters);
        var d;
        var e = 0;
        for (i = 0; i < c.length; i++) {
            d = parseTerms(c[i]);
            this.wildcard = (d.length == 1);
            for (j = 0; j < d.length; j++) {
                if (d[j] != '') {
                    a.push(new SearchTerm(e++, d[j].toLowerCase(), this.wildcard));
                }
            }
        }
        a.sort(function(a, b) {
            return ((a.term == b.term) ? 0 : ((a.term < b.term) ? -1 : 1));
        });
        var f = new SearchTerm(0, '');
        for (i = a.length - 1; i >= 0; i--) {
            if (a[i].term != f.term) {
                m.push(a[i]);
                f = a[i];
            }
        }
        m.sort(function(a, b) {
            return a.index - b.index;
        });
        debugAlert('Search Terms:\n' + m.join('\n'));
    }
    function parseTerms(a) {
        var s = a + '';
        s = s.replace(/(^|\s)(site|related|link|info|cache):[^\s]*(\s|$)/ig, ' ');
        s = s.replace(/(^|\s)-/g, ' ');
        s = s.replace(/\b(and|not|or)\b/ig, ' ');
        s = s.replace(/[,;]/ig, ' ');
        s = s.replace(/\s[a-z0-9]\s/ig, ' ');
        s = s.replace(/\s[a-z0-9]\s/ig, ' ');
        return s.split(/\s+/);
    }
    function getParamValues(a, b) {
        var c = new Array();
        var p = b.replace(/,/, ' ').split(/\s+/);
        if (a.indexOf('?') > 0) {
            var d = a.substr(a.indexOf('?') + 1);
            var e = d.split('&');
            for (i = 0; i < e.length; i++) {
                nameValue = e[i].split('=');
                if (nameValue.length != 2)
                    continue;
                for (j = 0; j < p.length; j++) {
                    if (nameValue[0] == p[j]) {
                        var f = decodeURIComponent(nameValue[1]).toLowerCase().replace(/\+/g, ' ');
                        f = f.replace(/a/ig, '[aáàảãạăắằẳẵặâấầẩẫậ]');
                        f = f.replace(/d/ig, '[dđ]');
                        f = f.replace(/e/ig, '[eéèẻẽẹêếềểễệ]');
                        f = f.replace(/i/ig, '[iíìỉĩị]');
                        f = f.replace(/o/ig, '[oóòỏõọôốồổỗộơớờởỡợ]');
                        f = f.replace(/u/ig, '[uúùủũụưứừửữự]');
                        f = f.replace(/y/ig, '[yýỳỷỹỵ]');
                        c.push(f);
                    }
                }
            }
        }
        return c
    }
    function highlight() {
        if (!k)
            return;
        if (!l)
            this.init();
        m.sort(function(a, b) {
            return (b.term.length - a.term.length);
        });
        for (var i = 0; i < m.length; i++) {
            this.highlightTerm(document.getElementById("searchResults"), m[i]);
        }
    }
    function highlightTerm(a, b) {
        if (a.nodeType == 1) {
            if (a.getAttribute("sth_x") == b.term)
                return;
            else
                a.setAttribute("sth_x", b.term);
        }
        if (a.hasChildNodes()) {
            for (var i = 0; i < a.childNodes.length; i++) {
                this.highlightTerm(a.childNodes[i], b);
            }
        }
        if (a.nodeType == 3) {
            var p = a.parentNode;
            if (p.nodeName != 'TEXTAREA' && p.nodeName != 'SCRIPT' && p.className.substr(0, b.CSS_CLASS_PREFIX.length) != b.CSS_CLASS_PREFIX) {
                var c = b.pattern.exec(a.nodeValue);
                if (c != null) {
                    b.found = true;
                    n++;
                    var d = c.index + (b.p_start == '[\\s,\\.]' ? 1 : 0);
                    var e = c.index + c[0].length - (b.p_end == '[\\s,\\.]' ? 1 : 0);
                    var v = a.nodeValue;
                    var f = document.createTextNode(v.substr(0, d));
                    var g = document.createTextNode(v.substr(e));
                    var h = document.createElement('SPAN');
                    h.className = b.cssClass;
                    h.appendChild(document.createTextNode(c[0].replace(/[\s,\.]/ig, '')));
                    p.insertBefore(f, a);
                    p.insertBefore(h, a);
                    p.replaceChild(g, a);
                }
            }
        }
    }
    function unhighlight() {
        var a = m[0].CSS_CLASS_PREFIX;
        var b = document.getElementsByTagName('SPAN');
        var c = new Array();
        var d, i, j;
        for (i = 0; i < b.length; i++) {
            if (b[i].className.substr(0, a.length) == a) {
                c.push(b[i]);
            }
        }
        for (i = 0; i < c.length; i++) {
            d = c[i].parentNode;
            for (j = 0; j < c[i].childNodes.length; j++) {
                d.insertBefore(c[i].childNodes[j], c[i]);
            }
            d.removeChild(c[i]);
        }
    }
    function debugAlert(a) {
        if (t)
            alert(a);
    }
    function SearchTerm(a, b, c) {
        this.CSS_CLASS_PREFIX = 'sth_';
        this.index = a;
        this.term = b.toLowerCase();
        this.cssClass = this.CSS_CLASS_PREFIX + this.term.replace(/[^a-z0-9_ ]/g, '').replace(/ /g, '_');
        this.p_start = '\\b';
        this.p_end = '\\b';
        if (this.term != null && this.term.length > 0) {
            if (this.term[0].match(/[^a-z0-9_]/) != null || this.term[0] == '[')
                this.p_start = '[\\s,\\.]';
            if (this.term[this.term.length - 1].match(/[^a-z0-9_\]]/) != null || this.term[this.term.length - 1] == ']')
                this.p_end = (c ? '' : '[\\s,\\.]');
        }
        this.pattern = new RegExp(this.p_start + this.term + this.p_end, 'i');
        this.found = false;
        this.toString = function() {
            return this.term;
        }
    }
    function HighlightStyle(a, b, c) {
        this.color = a;
        this.background = b;
        this.fontWeight = c;
        this.toString = function() {
            return 'color:' + this.color + '!important;background:' + this.background + '!important;font-weight:' + this.fontWeight + '!important;';
        }
    }
}